<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<div class="page-header">
    <button id="btnAdd" type="button" class="btn btn-primary btn-sm">
        <i class="fa fa-user-plus"></i>&nbsp;添加
    </button>
    <button id="btnEdit" type="button" class="btn btn-success btn-sm">
        <i class="fa fa-pencil-square-o"></i>&nbsp;编辑
    </button>
    <button id="btnDel" type="button" class="btn btn-danger btn-sm">
        <i class="fa fa-trash-o"></i>&nbsp;删除
    </button>
    <button id="btnResetPWD" type="button" class="btn btn-warning btn-sm">
        <i class="fa fa-unlock-alt"></i>&nbsp;重置密码
    </button>
    <button id="btnLock" type="button" class="btn btn-info btn-sm">
        <i class="fa fa-lock"></i>&nbsp;锁定
    </button>
    <button id="btnUnlock" type="button" class="btn btn-success btn-sm">
        <i class="fa fa-unlock"></i>&nbsp;解锁
    </button>
</div>
<%--<div class="input-group">
     <input id="searchKey" type="text" class="input form-control" placeholder="姓名、账户名称...">
     <span class="input-group-btn">
         <button id="btnSearch" class="btn btn-primary btn-sm" type="button"> <i class="fa fa-search"></i> 搜索</button>
     </span>
</div>--%>
<div class="row" style="margin-top:5px;">
    <div class="col-xs-12 widget-container-col ui-sortable"
         style="min-height: 127px;">
        <!-- #section:custom/widget-box.options.transparent -->
        <div class="widget-box transparent ui-sortable-handle"
             style="opacity: 1; z-index: 0;">
            <div class="widget-header">
                <h4 class="widget-title lighter">用户列表</h4>
                <div class="widget-toolbar no-border">
                    <a href="#" data-action="fullscreen" class="orange2">
                        <i class="ace-icon fa fa-arrows-alt"></i>
                    </a>
                    <a href="#" data-action="collapse" class="green">
                        <i class="ace-icon fa fa-chevron-up"></i>
                    </a>
                </div>
            </div>

            <div class="widget-body" style="display: block;">
                <div class="widget-main padding-6 no-padding-left no-padding-right">
                    <input id="pageNum" type="hidden" value="${page.pageNum }">
                    <input id="pageSize" type="hidden" value="${page.pageSize }">
                    <input id="orderByColumn" type="hidden" value="${page.orderByColumn }">
                    <input id="orderByType" type="hidden" value="${page.orderByType }">
                    <div id="dtGridContainer" class="dlshouwen-grid-container"></div>
                    <div id="dtGridToolBarContainer" class="dlshouwen-grid-toolbar-container"></div>
                </div>
            </div>
        </div>
    </div>

</div>

<script type="text/javascript">
    var dtGridColumns = [{
        id: 'serial',
        title: '序号',
        type: 'number',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return (grid.pager.nowPage - 1) * grid.pager.pageSize + dataNo + 1;
        }
    }, {
        id: 'accountName',
        title: '账户名',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="mailto:' + value + '">' + value + '</a>';
        }
    }, {
        id: 'userName',
        title: '姓名',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        fastQuery: true,
        fastQueryType: 'lk'
    }, {
        id: 'roleName',
        title: '所属角色',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'xs',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (/*typeof(value) == "undefined" ||*/ "" === value || null === value) {
                return '未指定';
            } else {
                return value;
            }
        }
    }, {
        id: 'deleteStatus',
        title: '是否删除',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs',
        codeTable: {0: "正常", 1: "已删除"},
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value === 0) {
                return '<span class="label label-sm label-success arrowed arrowed-righ">正常</span>';
            } else {
                return '<span class="label label-sm label-warning arrowed arrowed-righ">已删除</span>';
            }
        }
    }, {
        id: 'locked',
        title: '是否锁定',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs',
        codeTable: {0: "正常", 1: "锁定"},
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value === 0) {
                return '<span class="label label-sm label-success arrowed arrowed-righ">正常</span>';
            } else {
                return '<span class="label label-sm label-warning arrowed arrowed-righ">锁定</span>';
            }
        }
    }, {
        id: 'creatorName',
        title: '创建者',
        type: 'string',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs'
    }, {
        id: 'createTime',
        title: '创建时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        otype: 'string',
        oformat: 'yyyy-MM-dd hh:mm:ss',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs'
    }, {
        id: 'updateTime',
        title: '更新时间',
        type: 'date',
        format: 'yyyy-MM-dd hh:mm:ss',
        otype: 'string',
        oformat: 'yyyy-MM-dd hh:mm:ss',
        columnClass: 'text-center',
        headerClass: 'dlshouwen-grid-header',
        hideType: 'sm|xs|md',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value === null) {
                return '';
            } else {
                return value;
            }
        }
    }];

    //动态设置jqGrid的rowNum
    var pageSize = $("#pageSize").val();
    pageSize = pageSize == 0 || pageSize == "" ? sys.pageNum : pageSize;

    var dtGridOption = {
        lang: 'zh-cn',
        ajaxLoad: true,
        check: true,
        checkWidth: '37px',
        extraWidth: '37px',
        loadURL: sys.rootPath + '/user/list.html',
        columns: dtGridColumns,
        gridContainer: 'dtGridContainer',
        toolbarContainer: 'dtGridToolBarContainer',
        tools: 'fastQuery|refresh|print|export[excel]',
        exportFileName: '用户信息',
        pageSize: pageSize,
        pageSizeLimit: [10, 20, 30]
    };

    var grid = $.fn.dlshouwen.grid.init(dtGridOption);

    $(function () {
        if (null != $("#orderByColumn").val() && '' != $("#orderByColumn").val()) {
            grid.sortParameter.columnId = $("#orderByColumn").val();
            grid.sortParameter.sortType = $("#orderByType").val();
        }

        grid.load();

        initButtonClick();
    });


    /**
     * 初始化绑定按钮事件
     */
    function initButtonClick() {
        $("#btnAdd").click(function () {
            movic.common.addModel('/user/addUI.html');
        });

        $("#btnEdit").click(function () {
            movic.common.editModel('/user/editUI.html');
        });

        $("#btnDel").click(function () {
            delModel('/user/deleteBatch.html', customSearch);
        });

        $("#btnResetPWD").click(function () {
            resetPWDModel();
        });

        $("#btnLock").click(function () {
            lockModel('/user/lock.html');
        });

        $("#btnUnlock").click(function () {
            lockModel('/user/unlock.html');
        });

        $("#btnSearch").click(customSearch);

        //注册回车键事件
        /*document.onkeypress = function(e){
            var ev = document.all ? window.event : e;
            if(ev.keyCode == 13) {
                customSearch();
            }
        };*/
    }

    /**
     * 自定义查询
     * 这里不传入分页信息，防止删除记录后重新计算的页码比当前页码小而导致计算异常
     */
    function customSearch() {
        grid.parameters = {};
        grid.parameters['userName'] = $("#searchKey").val();
        grid.refresh(true);
    }

    /**
     *重置密码
     */
    function resetPWDModel() {
        var rows = grid.getCheckedRecords();
        if (rows.length == 1) {
            if (rows[0].role.key == 'administrator') {
                layer.msg('该用户为超级管理员,不能重置密码!', {
                    icon: 0
                });
                return false;
            }
            var index;
            $.ajax({
                type: "POST",
                url: sys.rootPath + '/user/resetPassword.html',
                data: {
                    "id": rows[0].id,
                    "accountName": rows[0].accountName,
                    "userName": rows[0].userName
                },
                dataType: "json",
                beforeSend: function () {
                    index = layer.load();
                },
                success: function (result) {
                    layer.close(index);
                    if (result.success === 200) {
                        layer.msg(result.message, {
                            icon: 1
                        });
                    } else {
                        layer.msg(result.message, {
                            icon: 5
                        });
                    }
                },
                error: function (data) {
                    layer.close(index);
                    layer.msg(data.responseText, {icon: 2});
                }
            });
        } else {
            layer.msg("你没有选择行或选择了多行数据", {
                icon: 0
            });
        }
    }

    /**
     *锁定
     */
    function lockModel(url) {
        var rows = grid.getCheckedRecords();
        if (rows.length === 1) {
            if (url == '/user/lock.html' || url == '/user/unlock.html') {
                if (rows[0].role.key === 'administrator') {
                    layer.msg('该用户为超级管理员,不能锁定或解锁!', {
                        icon: 0
                    });
                    return false;
                }
            }
            var index;
            $.ajax({
                type: "POST",
                url: sys.rootPath + url,
                data: {
                    "id": rows[0].id
                },
                dataType: "json",
                beforeSend: function () {
                    index = layer.load();
                },
                success: function (result) {
                    layer.close(index);
                    if (result.code == 200) {
                        layer.msg(result.message, {
                            icon: 1
                        });
                        movic.common.loadPage("/user/listUI.html");
                    } else {
                        layer.msg(result.message, {
                            icon: 5
                        });
                    }
                },
                error: function (data) {
                    layer.close(index);
                    layer.msg(data.responseText, {icon: 2});
                }
            });
        } else {
            layer.msg("你没有选择行或选择了多行数据", {
                icon: 0
            });
        }
    }

    /**
     * 删除
     */
    function delModel(nav, callback) {
        var rows = grid.getCheckedRecords();

        if (rows.length >= 1) {
            layer.confirm('确认删除吗？', {
                icon: 3,
                title: '删除提示'
            }, function (index, layero) {
                var delete_ids = [];
                for (var i = 0; i < rows.length; i++) {
                    delete_ids.push(rows[i].id);
                }
                var loadUI;
                $.ajax({
                    type: "POST",
                    url: sys.rootPath + nav,
                    data: {
                        "ids": delete_ids.join(',')
                    },
                    dataType: "json",
                    beforeSend: function () {
                        loadUI = layer.load();
                    },
                    success: function (result) {
                        layer.close(loadUI);
                        if (result.code === 200) {
                            layer.msg(result.message, {
                                icon: 1
                            });
                            if (callback) {
                                callback();
                            }
                        } else {
                            layer.msg(result.message, {
                                icon: 5
                            });
                        }
                    },
                    error: function (errorMsg) {
                        layer.close(loadUI);
                        layer.msg('服务器未响应,请稍后再试', {
                            icon: 3
                        });
                    }
                });
                layer.close(index);
            })

        } else {
            layer.msg("你没有选择数据", {
                icon: 0
            });
        }
    }

</script>

